﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Application.Dtos;
using System.Linq.Dynamic.Core;
using System.Net.Http;
using System.Net.Http.Json;
using Himp.Outpatient;
using System.Xml.Linq;
using Himp.Base;
using Himp.Platform;

namespace Ocean.Outpatient
{
    /// <summary>
    /// 门诊挂号记录服务
    /// </summary>
    public class OtpRgstRecService : OutpatientAppService, IOtpRgstRecService
    {
        private readonly IRepository<OtpRgstRec, Guid> _optRegRepository;
        private IOtpOrdRecService _otpOrdRecService;
        private IOtpRgstTypeService _otpRgstTypeService;
        private readonly OtpRgstManager _optRegManager;
        private readonly IRepository<OtpRgstOrd, Guid> _otpRgstOrdRepository;

        //private readonly IDistributedEventBus _distributedEventBus;
        //private readonly OtpRgstSagaManager _otpRgstSagaManager;
        //private readonly IHttpClientFactory _httpClientFactory;

        public OtpRgstRecService(IRepository<OtpRgstRec, Guid> optRegRepository
            , IOtpOrdRecService otpOrdRecService
            , IOtpRgstTypeService otpRgstTypeService
            , OtpRgstManager optRegManager
            , IRepository<OtpRgstOrd, Guid> otpRgstOrdRepository)
        {
            _optRegRepository = optRegRepository;
            _optRegManager = optRegManager;
            _otpOrdRecService = otpOrdRecService;
            _otpRgstTypeService = otpRgstTypeService;
            _otpRgstOrdRepository = otpRgstOrdRepository;
        }

        /// <summary>
        /// 获取挂号登记记录列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OtpRgstRecDto>> GetListAsync(OtpRgstRecInput input)
        {
            var iQuery = await _optRegRepository.GetQueryableAsync();
            iQuery = iQuery.OrderByDescending(i => i.CreationTime);

            //input.BegnSDateTime = input.BegnSDateTime ?? DateTime.Now.Date;
            //input.BegnEDateTime = input.BegnEDateTime ?? DateTime.Now.Date;
            //input.BegnEDateTime = input.BegnEDateTime.Value.AddDays(1).AddSeconds(-1);
            //iQuery = iQuery.Where(i => i.Rgsttime.Date >= input.BegnSDateTime);
            //iQuery = iQuery.Where(i => i.Rgsttime.Date <= input.BegnEDateTime);
            //iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.PsnName), i => i.PsnName.Contains(input.PsnName));
            //iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.OtpNo), i => i.OtpNo.Contains(input.OtpNo));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpRgstRec>, List<OtpRgstRecDto>>(iQuery.ToList());

                return new PagedResultDto<OtpRgstRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpRgstRec>, List<OtpRgstRecDto>>(items.ToList());

                return new PagedResultDto<OtpRgstRecDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<OtpRgstRecDto> GetAsync(Guid id)
        {
            var optReg = await _optRegRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<OtpRgstRec, OtpRgstRecDto>(optReg);

            return itemDto;
        }

        /// <summary>
        /// 门诊挂号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns> 
        public async Task<OtpOrdRecDto> PostAsync(OtpRgstRecCreateDto input)
        {
            //获取挂号类别
            var otpRgstType = await _otpRgstTypeService.GetAsync(input.OtpRgstTypeId);
            OtpOrdRecCreateDto otpOrdRecCreateDto = new OtpOrdRecCreateDto();
            otpOrdRecCreateDto.OrdBizSouc = EnumOrdBizSouc.门诊挂号;
            otpOrdRecCreateDto.SetlTypeCode = input.SetlTypeCode;
            otpOrdRecCreateDto.SetlTypeName = input.SetlTypeName;
            otpOrdRecCreateDto.TotAmt = otpRgstType.Amt;
            otpOrdRecCreateDto.DiscAmt = 0;
            otpOrdRecCreateDto.AdvAmt = 0;
            otpOrdRecCreateDto.RecAmt = otpRgstType.Amt;
            otpOrdRecCreateDto.SelfpayAmt = otpRgstType.Amt;
            var otpOrdRec = await _otpOrdRecService.PostAsync(otpOrdRecCreateDto);

            var optReg = _optRegManager.CreateOtpRgst(input.SetlTypeCode, input.SetlTypeName
                , input.PatnId, input.Age, input.Gender, input.OtpNo
                , input.PatnName, input.OtpSchdRecId, input.OtpSchdRecDetlId
                , input.OtpRgstTypeId, input.RgstDate, input.DeptCodg, input.DeptName
                , input.DrCodg, input.DrName, input.MdtrtCertTypeCode, input.MdtrtCertNo);
            optReg = await _optRegRepository.InsertAsync(optReg);

            OtpRgstOrd otpRgstOrd = new OtpRgstOrd();
            otpRgstOrd.OrdNo = otpOrdRec.OrdNo;
            otpRgstOrd.OtpOrdRecId = otpOrdRec.Id;
            otpRgstOrd.OtpRgstRecId = optReg.Id;
            otpRgstOrd.Invono = otpOrdRec.Invono;
            await _otpRgstOrdRepository.InsertAsync(otpRgstOrd);

            return otpOrdRec;

            //var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(itemDto), Encoding.UTF8, "application/json");
            //var client = _httpClientFactory.CreateClient(nameof(OtpRgstRecService));
            //var responseModel = await client.PostAsync($"https://localhost:7048/workflows/Otp-Rgst-Reg", content);

            //if (responseModel.StatusCode == System.Net.HttpStatusCode.OK)
            //{
            //    var j = await responseModel.Content.ReadAsStringAsync();
            //    var ordInfoDto = await responseModel.Content.ReadFromJsonAsync<OrdInfoDto>();
            //    optReg.OrderId = ordInfoDto.Id;
            //    itemDto.OrderId = ordInfoDto.Id;
            //}
            //else
            //{

            //}

            //var otpRgstRecCreatedEto = ObjectMapper.Map<OtpRgstRec, OtpRgstRecCreatedEto>(optReg);
            //await _distributedEventBus.PublishAsync(otpRgstRecCreatedEto);

            //return itemDto;
        }
    }
}
